// pages/community/my/mydiscuss.js
var util = require('../../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discussList: [],
    currentPage: 1,//当前页数 
    offline:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDiscussList(true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   //获得dialog组件
    this.loadmore = this.selectComponent("#loadmore");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    this.setData({
      currentPage: 1
    })
    this.getDiscussList(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadmore.show();
    if (this.data.hasmore) {
      const pagenum = this.data.currentPage + 1
      this.setData({
        currentPage: pagenum
      })
    }
    this.getDiscussList(false);
  },
  //获取帖子列表
  getDiscussList: function (isRefresh) {
    const that = this;
    const pagenum = this.data.currentPage;
    const discussType = this.data.discussType;
    var discussList = this.data.discussList;
    const accountid = app.globalData.accountid;
    var url = '';
    var imgurl = '';
    const params = "accountid=" + accountid;
    //最热
    url = "/apid/r/142/" + pagenum;
    imgurl = "/apid/r/143/" + pagenum;
  
    //获取帖子列表
    util.DoCommand(url, params, null, function (data) {
      var pagenum = that.data.currentPage;
      var hasmore = true;
      if (data.length < app.globalData.pagesize) {
        hasmore = false;
      }
      that.setData({
        hasmore: hasmore
      })
      //获取帖子图片
      util.DoCommand(imgurl, params, null, function (imgdata) {
        for (var i in data) {
          var _discuss = data[i];
          _discuss.discussImage = [];
          for (var j in imgdata) {
            var _img = imgdata[j];
            if (_img.parentId == _discuss.id) {
              _discuss.discussImage.push(_img.imgUrl)
            }
          }
        }
        if (isRefresh == true) {
          discussList = data
        }
        else {
          //合并数据
          for (var i in data) {
            var _id = data[i].id;
            var hasCount = 0;//标识是否已存在
            for (var j in discussList) {
              var _nid = discussList[j].id;
              if (_nid == _id) {
                hasCount++;
              }
            }
            //如果不存在则追加数据
            if (hasCount < 1) {
              discussList.push(data[i]);
            }
          }
        }

        that.setData({
          discussList: discussList,
        });
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
        that.loadmore.hide()
      }, null, that)
    }, null, that)
  },
  openDetail: function (options) {
    const that=this;
    var disscussData = that.data.discussList;
    const currentIndex = options.target.dataset.index;
    var id = options.target.dataset.discussid;
    const accountid = app.globalData.accountid;
    const path = "../../../pages/community/detail/detail?id=" + id;
    const newid = util.NewGuid();
    const url = "/apid/c/113";
    const params = "discussid=" + id + "&accountid=" + accountid + "&id=" + newid
    //调用接口记录阅读状态
    util.DoCommand(url, params, null, function (data) {
      disscussData[currentIndex].count_read++;
      that.setData({
        discussList: disscussData
      })
    }, null, that)
    wx.navigateTo({
      url: path
    })
  },
  //收藏
  tapFavorite: function (e) {
    const that = this;
    const isfavorite = e.currentTarget.dataset.isfavorite;
    const currentIndex = e.currentTarget.dataset.index;
    const discussid = e.currentTarget.dataset.discussid;
    const accountid = app.globalData.accountid;
    const currentData = this.data.discussList;
    var url = "";
    var params = null;
    //如果已收藏则将收藏状态改为为收藏
    if (isfavorite == 1) {
      //取消收藏 
      currentData[currentIndex].isfavorite = 0;
      url = "/apip/discuss/nofavorite/" + discussid + "/" + accountid
    }
    //如果未收藏则将收藏状态改为已收藏
    else {
      //收藏 
      currentData[currentIndex].isfavorite = 1;
      const id = util.NewGuid();
      // url = "/apid/c/111?discussid=" + discussid + "&accountid=" + accountid + "&id=" + id;
      url = "/apid/c/111";
      params = "discussid=" + discussid + "&accountid=" + accountid + "&id=" + id
    }
    this.setData({
      discussList: currentData
    })
    //调用接口记录收藏状态
    util.DoCommand(url, params, null, function (data) { }, null, that)
  },
  //点赞
  tapSupport: function (e) {
    const that = this;
    const issupport = e.currentTarget.dataset.issupport;
    const currentIndex = e.currentTarget.dataset.index;
    const discussid = e.currentTarget.dataset.discussid;
    const accountid = app.globalData.accountid;
    const currentData = this.data.discussList;
    var url = "";
    var params = null;
    //如果已支持则将收藏状态改为未支持
    if (issupport == 1) {
      //取消点赞
      currentData[currentIndex].issupport = 0;
      currentData[currentIndex].count_support = parseInt(currentData[currentIndex].count_support) - 1;
      url = "/apip/discuss/nosupport/" + discussid + "/" + accountid
    }
    //如果未支持则将收藏状态改为已支持
    else {
      //点赞
      currentData[currentIndex].issupport = 1;
      currentData[currentIndex].count_support = parseInt(currentData[currentIndex].count_support) + 1;
      const id = util.NewGuid();
      url = "/apid/c/112";
      params = "discussid=" + discussid + "&accountid=" + accountid + "&id=" + id;
    }
    this.setData({
      discussList: currentData
    })
    //调用接口记录点赞状态
    util.DoCommand(url, params, null, function (data) { }, null, that)
  },
  //预览图片
  previewImage: function (e) {
    const currentIndex = e.currentTarget.dataset.index;
    const currentSrc = e.currentTarget.dataset.src;
    const imgList = this.data.discussList[currentIndex].discussImage;
    wx.previewImage({
      current: currentSrc, // 当前显示图片的http链接  
      urls: imgList // 需要预览的图片http链接列表  
    })
  },
  onShareAppMessage: function (options) {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      success: function (res) {
        // 转发成功之后的回调-
        if (res.errMsg == 'shareAppMessage:ok') {
          //页面内的按钮的转发
          if (options.from == 'button') {
            //记录分享次数
            const accountid = app.globalData.accountid;
            const discussid = options.target.dataset.discussid
            const id = util.NewGuid();
            const url = "/apid/c/132";
            const params = "discussid=" + discussid + "&accountid=" + accountid + "&id=" + id
            util.DoCommand(url, params, null, function (data) { }, null, that)
          }

        }
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调（转发成不成功都会执行）
      }
    }
    // 来自页面内的按钮的转发
    if (options.from == 'button') {
      //帖子id
      var discussid = options.target.dataset.discussid;
      shareObj.path = '/pages/community/detail/detail?id=' + discussid;
    }
    // 返回shareObj
    return shareObj;
  }, //更改帖子数据---详情页调用
  changeData: function (id, issupport, isfavorite, count_reply) {
    //帖子列表
    var disscussData = this.data.discussList;
    for (var i in disscussData) {
      if (disscussData[i].id == id) {
        if (isfavorite != 'none') {
          disscussData[i].isfavorite = isfavorite;
        }
        if (issupport != 'none') {
          if (issupport == 0) disscussData[i].count_support--;
          if (issupport == 1) disscussData[i].count_support++;
          disscussData[i].issupport = issupport;

        }
        if (count_reply != 'none') {
          disscussData[i].count_reply = count_reply;
        }
        //    return;
      }
    }
    this.setData({
      discussList: disscussData
    })
  }
  
})